package jplat.core.session;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import jplat.base.constant.KPlatResponseCode;
import jplat.core.session.redis.JRedisSession;
import jplat.core.trans.JAppConnectInfo;
import jplat.error.exception.JSystemException;
import jplat.tools.config.JAppConfig;
import jplat.tools.string.StringUtil;

public class JSessionUtils
{
	/**
	 * 获取Redis会话ID.
	 * @author zhangcq
	 * @date Apr 11, 2017
	 * @comment 
	 * @param request
	 * @return
	 */
	public static String obtSessionId( HttpServletRequest request )
	{
		Map<String,String> cookMap = new HashMap<String,String>();

		cookMap.put(JAppConnectInfo.H_HTOKEN_NAME, request.getHeader(JAppConnectInfo.H_HTOKEN_NAME));

		//从Cookie中获取:所以,如果值重复,最后是以Cookie中的为准.
		Cookie[] cookies = request.getCookies();
		if ( cookies != null )
		{
			for ( int i = 0; i < cookies.length; ++i )
			{
				cookMap.put(cookies[i].getName(), cookies[i].getValue());
			}
		}

		return cookMap.get(JAppConnectInfo.H_HTOKEN_NAME);
	}

	/**
	 * 检测会话是否有效.
	 * @author zhangcq
	 * @date Apr 11, 2017
	 * @comment 
	 * @param request
	 * @return
	 */
	public static JSession checkSessionAvail( HttpServletRequest request )
	{
		try
		{
			return createSession( request, JAppConfig.SESSION_TYPE,false);
		}
		catch (JSystemException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			return null;
		}
	}

	/**
	 * 创建会话,一定是在需要的时候调用.
	 * @author zhangcq
	 * @date Apr 11, 2017
	 * @comment 
	 * @param request
	 * @param type
	 * @param create
	 * @return
	 * @throws JSystemException
	 */
	public static JSession createSession( HttpServletRequest request, int type, boolean create ) throws JSystemException
	{
		switch( type )
		{
		case JSession.SESS_HTTP:
			return new JHttpSession(request,create);
		case JSession.SESS_REDIS:
			if ( create )
			{
				return new JRedisSession();
			}

			return new JRedisSession( obtSessionId(request) );
			
		default:
			throw new JSystemException(KPlatResponseCode.CD_CONF_ERROR, KPlatResponseCode.MSG_CONF_ERROR+"_cs");
		}
	}
}
